import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";

export default function useCusRouter<QK extends string[]>(options?: UseCusRouterOptions<QK>) {
  // 获取路由对象
  const router = useRouter();
  // 获取当前路由
  const route = useRoute();
  // 参数keys响应式处理
  const queryKeys = ref(options?.queryKeys || []);
  // 获取路由参数
  const routeQuery = computed<Record<QK[number], string>>(() => queryKeys.value.reduce((result, target) => {
    result[target] = route.query[target] as string;
    return result;
  }, {} as Record<string, string>));

  return { router, routeQuery }
}

declare interface UseCusRouterOptions<QK extends string[] = []> {
  // 參數keys
  queryKeys: QK;
}
